package com.baitiaojun.video.support;

import lombok.Getter;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @类名 RedisSuport
 * @作者 白条君
 * @创建日期 2022/10/28 15:15
 * @版本 1.0
 */
@Getter
@Component("redisSupport")
public class RedisSupport {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public HashOperations<String, String, Object> hash() {
        return redisTemplate.opsForHash();
    }

    public ValueOperations<String, Object> string() {
        return redisTemplate.opsForValue();
    }

    public <T> void setHash(String key, String field, T value) {
        hash().put(key, field, value);
    }

    public Object getHash(String key, String field) {
        return hash().get(key, field);
    }

    public void delete(String key, String field) {
        hash().delete(key, field);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public void setValue(String key, Object value, long expire, TimeUnit timeUnit) {
        string().set(key, value, expire, timeUnit);
    }

    public Object getValue(String key) {
        return string().get(key);
    }

    public void increment(String key) {
        string().increment(key);
    }
}